﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
//using Microsoft.Extensions.Configuration;
using Psi.Domain;
//using MySQL.Data.EntityFrameworkCore.Extensions;
//using Pomelo.EntityFrameworkCore.MySql;
//using Pomelo.EntityFrameworkCore.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Configuration;
using Microsoft.Extensions.Configuration;

//UseSqlServer 获取DefaultConnection
//https://stackoom.com/question/369pg/%E4%BB%8Eappsettings-json%E8%8E%B7%E5%8F%96ConnectionString-%E8%80%8C%E4%B8%8D%E6%98%AF%E5%9C%A8-NET-Core-App%E4%B8%AD%E8%BF%9B%E8%A1%8C%E7%A1%AC%E7%BC%96%E7%A0%81

namespace Psi.Models
{
    public class PsiDbContextFactory : IDesignTimeDbContextFactory<PsiDbContext>
    {
        public PsiDbContext CreateDbContext(string[] args)
        {
            var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: false)
                .AddJsonFile($"appsettings.{envName}.json", optional: false)
                .Build();
            
            var builder = new DbContextOptionsBuilder<PsiDbContext>()
                .UseNpgsql(configuration.GetConnectionString("PgSQLConnection"));
                //.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));

            return new PsiDbContext(builder.Options);
        }
    }
}